package com.itany.nmms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.DictConstant;
import com.itany.nmms.entity.Dept;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.service.DeptService;
import com.itany.nmms.service.StaffService;
import com.itany.nmms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/14 14:05
 * Version:1.0-SNAPSHOT
 */
@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private DeptService deptService;

    @RequestMapping("/login")
    public ModelAndView login(String loginName, String password, String role, String code, HttpSession session){
        ModelAndView mav = new ModelAndView();

        String image = (String) session.getAttribute("code");
        try {
            Staff staff = staffService.login(loginName,password,role,code,image);
            session.setAttribute("staff",staff);
            mav.setViewName("redirect:/showMain");
        } catch (RequestParameterErrorException e) {
            mav.addObject("loginMsg",e.getMessage());
            mav.setViewName("backend/login");
        } catch (CodeErrorException e) {
            mav.addObject("loginMsg",e.getMessage());
            mav.setViewName("backend/login");
        } catch (StaffNotExistException e) {
            mav.addObject("loginMsg",e.getMessage());
            mav.setViewName("backend/login");
        } catch (Exception e) {
            e.printStackTrace();
            mav.addObject("loginMsg","服务器内部异常");
            mav.setViewName("backend/login");
        }

        return mav;
    }


    @RequestMapping("/findByParams")
    public ModelAndView findByParams(@ModelAttribute("staffParam") Staff staffParam, String pageNo, String pageSize){
        ModelAndView mav = new ModelAndView();

        if(ParameterUtil.isNull(pageNo)){
            pageNo = DictConstant.STAFF_PAGE_HELP_DEFAULT_PAGE_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize = DictConstant.STAFF_PAGE_HELP_DEFAULT_PAGE_SIZE;
        }

        List<Dept> depts = deptService.findEnable();
        mav.addObject("depts",depts);

        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        List<Staff> staffs = staffService.findByParams(staffParam);
        PageInfo<Staff> staffPage = new PageInfo<>(staffs);

        // 将查询到的数据保存到ModelMap中
        mav.addObject("staffPage",staffPage);
        // 将查询条件保存到ModelMap,用于分页操作中保留查询条件
//        mav.addObject("staffParam",staffParam);

        mav.setViewName("backend/staffManager");
        return mav;
    }

}
